﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using RepositoryUnit.Data.Entity;
using RepositoryUnit.Repository.Interface;
using RepositoryUnit.Repository.UnitOfWork;

namespace RepositoryUnit.Api.Controller
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IRepository<TbUser> _userRepository;
        private readonly IRepository<Teacher> _teacherRepository;
        private readonly IRepository<User> _newUserRepository;
        private readonly IUnitOfWork _unitOfWork;

        public UserController(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
            _userRepository = _unitOfWork.GetRepository<TbUser>();
            _teacherRepository = _unitOfWork.GetRepository<Teacher>();
            _newUserRepository = _unitOfWork.GetRepository<User>();
        }
        /// <summary>
        /// 创建用户
        /// </summary>
        /// <returns></returns>
        [Route("createUser")]
        [HttpPost]
        public TbUser CreateUser()
        {
            var user = new TbUser
            {
                UserId = Guid.NewGuid().ToString("N"),
                CreateTime = DateTime.Now,
                UserName = "tenghao",
                Email = "tenghao510@qq.com"
            };
            _userRepository.AddAsync(user);
            return user;
        }
        /// <summary>
        /// 查询用户
        /// </summary>
        /// <returns></returns>
        [Route("getUser")]
        [HttpGet]
        public async Task<IEnumerable<TbUser>> GetUser()
        {
            return await _userRepository.GetAllListWithNoTrackingAsync();
        }

        [HttpGet(nameof(TeacherAndUser))]
        public async Task TeacherAndUser()
        {
            #region include
            {
                //var teacherQueryable = _teacherRepository.Queryable;
                //var list = await teacherQueryable.Include(x => x.Users).AsNoTracking().ToListAsync();

                //var manyList = list.SelectMany(x => x.Users, (x, users) => new
                //{
                //    Id = x.Id,
                //    teacherName = x.Name,
                //    userName = users.Name
                //}).ToList();

                //var ll = manyList.GroupBy(x => new { x.Id, x.teacherName })
                //                 .Select(x => new
                //                 {
                //                     Id = x.Key.Id,
                //                     Name = x.Key.teacherName,
                //                     Users = manyList.Where(y => y.Id == x.Key.Id).Select(y => new
                //                     {
                //                         UserName = y.userName
                //                     }).ToList()
                //                 }).ToList();

                //var a = (from m in manyList
                //         group new { m.userName } by new { m.Id, m.teacherName } into K
                //         select new
                //         {
                //             Id = K.Key.Id,
                //             Name = K.Key.teacherName,
                //             Users = K.ToList()
                //         }).ToList();
            }
            #endregion


            #region MyRegion
            {
                var teacherRepository = _unitOfWork.GetRepository<Teacher>();
                var userRepository = _unitOfWork.GetRepository<User>();

                var teacherQueryable = teacherRepository.Queryable;

                teacherQueryable = teacherQueryable.Where(x => x.Name == "haha");

                Expression<Func<Teacher, bool>> expression = x => true;
                var list= await teacherRepository.GetListWithNoTrackingAsync(teacherQueryable);
                var list1 = await teacherRepository.GetListWithNoTrackingAsync(expression);

                var userQueryable = userRepository.Queryable;




                var teacherList = await teacherRepository.GetAllListWithNoTrackingAsync();
                var userList = await userRepository.GetAllListWithNoTrackingAsync();


                var a = await (from t in teacherQueryable
                               join u in userQueryable
                               on t.Id equals u.TeacherId
                               select new
                               {
                                   Teacher = t,
                                   User = u
                               }).ToListAsync();


                var b = (from t in teacherList
                         join u in userList
                         on t.Id equals u.TeacherId
                         select new
                         {
                             Teacher = t,
                             User = u
                         }).ToList();

                var a1 = await teacherQueryable.Join(userQueryable, t => t.Id, u => u.TeacherId, (t, u) => new
                {
                    Teacher = t,
                    User = u
                }).ToListAsync();

                var a2 = await (from t in teacherQueryable
                                join u in userQueryable
                                on t.Id equals u.TeacherId into UU
                                from u1 in UU.DefaultIfEmpty()
                                select new
                                {
                                    Teacher = t,
                                    User = u1
                                }).ToListAsync();


                //var teacherList = await teacherRepository.GetAllListWithNoTrackingAsync();
                //var userList = await userRepository.GetAllListWithNoTrackingAsync();


                //var teacherList = await _teacherRepository.GetAllListWithNoTrackingAsync();
                //var userList = await _newUserRepository.GetAllListAsync();

                //var list = teacherList.GroupJoin(userList, t => t.Id, u => u.TeacherId, (t, u) => new
                //{
                //    Id = t.Id,
                //    Name = t.Name,
                //    Users = u.ToList()
                //}).ToList();
            }
            #endregion
        }
    }
}
